package com.lyc.user.serlvet;

import com.lyc.user.bean.User;
import com.lyc.user.servceImpl.UserServiceImpl;
import com.lyc.user.service.UserService;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserPhoneLogin implements Servlet {
    private ServletConfig servletConfig;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
            this.servletConfig=servletConfig;
    }

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //得到参数，手机号和验证码
           String phone= servletRequest.getParameter("phone");
           String veriCode=servletRequest.getParameter("veriCode");
         //封装对象
        User u=new User();
        u.setPhone(phone);
        u.setVeriCode(veriCode);
        //调用业务层的手机号登陆方法
        UserService userService=new UserServiceImpl();
           String phoneLoginResult= userService.phoneLogin(u);

           servletResponse.setCharacterEncoding("UTF-8");
           servletResponse.setContentType("text/html");
           //得到登录的结果
           servletResponse.getWriter().print(phoneLoginResult);
    //	请求的重定向
        HttpServletResponse resp=(HttpServletResponse)servletResponse;
        if(phoneLoginResult.equals("登录成功！")){
            //  getServletContext() 获得：http://localhost:端口名
            //  getContextPath() 获得工程名 javaWeb01
            //重定向
            resp.sendRedirect(servletRequest.getServletContext().getContextPath()+"/bookServlet?methodName=getBookList");
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
